<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<c:set var="webroot" value="${pageContext.request.contextPath}" />
<html>
<head>
	<meta charset="utf-8">
	<title>添加成员</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="${webroot}/static/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="${webroot}/static/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form" style="width:80%;">
    <input type="hidden" id="id" name="id" value="${users.userId}">
    <input type="hidden" id="show" value="${show}">
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label"><span style="color:red;">*</span>成员账号:</label>
		<div class="layui-input-block">
			<input type="text" class="layui-input userId" id="userId" lay-verify="required" placeholder="请输入成员账号" value="${users.userId}">
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label"><span style="color:red;">*</span>成员姓名:</label>
		<div class="layui-input-block">
			<input type="text" class="layui-input userName" lay-verify="required" placeholder="请输入成员姓名" value="${users.userName}">
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label">邮箱地址:</label>
		<div class="layui-input-block">
			<input type="text" class="layui-input Email" lay-verify="NEWEmail" placeholder="请输入邮箱" value="${users.email}">
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label"><span style="color:red;">*</span>角色名称:</label>
		<div class="layui-input-block newsStatus">
			<select name="roleId" lay-verify="required">
			<c:forEach items="${list}" var="l">
			    <c:if test="${l.roleId == users.roleId}">
			        <option selected="selected" value="${l.roleId}">${l.roleName}</option>
			    </c:if>
			    <c:if test="${l.roleId != users.roleId}">
			        <option value="${l.roleId}">${l.roleName}</option>
			    </c:if>
			</c:forEach>
			</select>
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label"><span style="color:red;">*</span>登录密码:</label>
		<div class="layui-input-block">
			<input type="password" class="layui-input password" lay-verify="required" placeholder="请输入登录密码" value="${users.password}">
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label"><span style="color:red;">*</span>确认密码:</label>
		<div class="layui-input-block">
			<input type="password" class="layui-input upPassword" lay-verify="required" placeholder="请输入确认密码" value="${users.password}">
		</div>
	</div>

	<div class="layui-form-item layui-row layui-col-xs12">
		<div class="layui-input-block">
			<button class="layui-btn" lay-submit="" lay-filter="addUser">提交</button>	
			<%-- <c:if test="${users != null}">
	    	   <button type="reset" class="layui-btn layui-btn-primary" onclick="close_self();">取消</button>
	        </c:if> --%>
	        <button type="reset" class="layui-btn layui-btn-primary" onclick="close_self();">取消</button>
		</div>
	</div>
</form>
<script type="text/javascript" src="${webroot}/static/layui/layui.js"></script>
<script src="${webroot}/static/js/jquery.min.js" charset="utf-8"></script>
<script>
layui.use(['form','layer'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
    if($("#id").val() != ""){
    	$("#userId").attr("readonly","readonly");
    }
    
    form.verify({
    	NEWEmail : function(value, item){
    		if(value != null && value != ""){
    			var re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
    			if(!re.test(value)){
    				return '邮箱格式不正确';
    			}
    		}
    	}
	});
    form.on("submit(addUser)",function(data){
    	if($(".upPassword").val() != $(".password").val()){
    		top.layer.msg("俩次密码输入不统一！");
    		return false;
    	}
    	var url =""; var record="";
    	if($("#id").val() != ""){
    		url="/admin/user/update";
    		record="编辑成员:";
    	}else{
    		url="/admin/user/insert";
    		record="添加成员:";
    	}
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post(url,{
             userName : $(".userName").val(),  //登录名
             userId: $(".userId").val(),  //邮箱
             email: $(".Email").val(),  
             roleId : data.field.roleId, 
             password : $(".upPassword").val(),
             record : record
         },function(res){
        	 var data=JSON.parse(res);
        	 if(data.success){
        		top.layer.close(index);
  	            top.layer.msg("操作成功");
        		 setTimeout(function(){
        	            if($("#show").val()=="1"){
        	        		window.location.href="/admin/user/showuser";
        	        	}else{
        	        		window.location.href="/admin/user/dateil";
        	        	}
        	        },2000); 
        	 }else{
        		 top.layer.msg(data.message);
        	 }
        })
        return false;
    })

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());
})
function close_self(){
	if($("#show").val()=="1"){
		window.location.href="/admin/user/showuser";
	}else{
		window.location.href="/admin/user/dateil";
	}
}
</script>
</body>
</html>